package BM;

import SFRZ.RSA;
import org.apache.commons.codec.binary.Base64;

import java.security.KeyPair;
import java.security.PrivateKey;

/**
 * @author konley
 * @date 2021-12-11 12:53
 */
public class ReceiverB {
    private String puk;
    private String prk;

    //获取公钥
    public String getPukKey(){
        return puk;
    }

    public void getKey() throws Exception {
        //生成密钥对
        KeyPair keyPair = RSA.getKeyPair();
        prk = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
        puk = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
        System.out.println("私钥:" + puk);
        System.out.println("公钥:" + prk);
    }
    //解密模块
    public String decrypt(String y) throws Exception {
        PrivateKey privateKey = RSA.getPrivateKey(prk);
        //根据密文y解密得到m
        String m = RSA.decrypt(y,privateKey);
        return m;
    }
}
